package com.cskaoyan.service;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.vo.CollectVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

@Service
public class CollectServicelmpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    /**
     * 功能:会员收藏页面
     * 作者:whj
     */
    @Override
    public CollectVo query(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);
        //排序
        CollectExample collectExample = new CollectExample();
        collectExample.setOrderByClause(sort + " " + order);
        //根据userId,valueId进行精准查询
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if(userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId != null && !"".equals(valueId)){
            criteria.andValueIdEqualTo(valueId);
        }
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        //符合条件的total
        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();
        //返回结果
        CollectVo collectVo = new CollectVo();
        collectVo.setItems(collects);
        collectVo.setTotal(total);
        return collectVo;
    }
}
